import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

/**
 * 
 * This class is the parent controller
 *
 */
public class Parent implements Serializable{

	private String ID;
	private String Name;
	private LinkedList<String> childrens;
/**
 * 
 * @param iD
 * @param name
 * @param childrens
 */
	public Parent(String iD, String name, LinkedList<String> childrens) {
		super();
		ID = iD;
		Name = name;
		this.childrens = childrens;
	}

	public Parent() {
		super();
		// TODO Auto-generated constructor stub
	}

/**
 * 
 * @param id
 */
	public Parent(String id) {
		Parent P = DBparentData(id);
		ID = id;
		this.Name = P.Name;
		this.childrens = P.childrens;
	}

/**
 * 
 * @return
 */
	public String getID() {
		return ID;
	}
	/**
	 * 
	 * @return
	 */
	public String getName() {
		return Name;
	}
/**
 * 
 * @return
 */
	public LinkedList<String> getChildrens() {
		return childrens;
	}
	/**
	 * 
	 * @param id
	 * @return
	 */
	public Parent DBparentData(String id)
	{
		SQL s1 = new SQL(id,99);
		s1.accept();
		String Name = (String)s1.getMsg();
		
		SQL s2 = new SQL(id,100);
		s2.accept();
		LinkedList<String> Childs= (LinkedList<String>)s2.getMsg();
		Parent P = new Parent(id,Name,Childs);
		return P;
	}
	/**
	 * 
	 * @return List of this parent children's grades
	 */
	public LinkedList<LinkedList<Grades>> DBgrades()
	{
		Grades G = new Grades();
		LinkedList<LinkedList<Grades>> list = new LinkedList<LinkedList<Grades>>();
		Iterator<String> itr = this.childrens.iterator();
		while (itr.hasNext())
			list.add(G.DBgetGrades(itr.next()));
		return list;
	}
	
	public void DBSHOW()
	{
		System.out.println("Now Parents data should show");

		LinkedList<LinkedList<Grades>>list = DBgrades();
		Iterator<LinkedList<Grades>> itrCgrades = list.iterator();
		LinkedList<String> childID = getChildrens();
		Iterator<String> itrChild = childID.iterator();
		int count = 1;

		/////////////////////////Start of Print area////////////////////////////////
		
		String output = "";
		if (!itrCgrades.hasNext()) output += "This parent has no childrens in this school\n";
		else
		{
			while (itrCgrades.hasNext())
			{
				output +="Here are the grades of child "+count+": "+ itrChild.next()+"\n";
				count++;
				Iterator<Grades> itrGrades = itrCgrades.next().iterator();
				if (!itrGrades.hasNext()) output += "This student has no grades yet\n";
				else
				{
					while (itrGrades.hasNext())
					{
						Grades G = itrGrades.next();
						output += G.getStudentID()+" "+G.getCourseID()+" "+G.getGrade()+"\n";
					}	
				}
			}
		}
		JTextArea outputArea = new JTextArea( 11, 60 );
		outputArea.setText( output );

		JOptionPane.showMessageDialog( null, outputArea,"Parent Data",JOptionPane.INFORMATION_MESSAGE );
		
		/////////////////////////End of Print area////////////////////////////////

	}
}
